package com.hellpro.lolbox.home.weight;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.bean.ZXZp;
import com.hellpro.lolbox.home.presenter.ZXRecycleAdapter;
import com.hellpro.lolbox.home.presenter.ZXRecycleBiz;
import com.hellpro.lolbox.home.view.IZXRecycleView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ZXCommBFragment extends BaseFragment implements IZXRecycleView{
private  int page ;


    @BindView(R.id.xrecycleview)
    XRecyclerView xrecycleview;
    private ZXRecycleAdapter zxRecycleAdapter;
    private ZXRecycleBiz zxRecycleBiz;




    @Override
    public void initView() {
        //设置管理器
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
         xrecycleview.setLayoutManager(staggeredGridLayoutManager);
        zxRecycleAdapter = new ZXRecycleAdapter();
        xrecycleview.setAdapter(zxRecycleAdapter);
        zxRecycleBiz = new ZXRecycleBiz(this);
        Bundle arguments = getArguments();

        final String  mPath= arguments.getString("path");
        page =1;
       String path = mPath +page;
        down(mPath +page);
        //刷新
        xrecycleview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
               zxRecycleAdapter.reMove();
          down(mPath +1);
                xrecycleview.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrecycleview.refreshComplete();
                    }
                },3000);
            }

            @Override
            public void onLoadMore() {
                if (page ==11){
                    xrecycleview.loadMoreComplete();
                    return;
                }
                page++;
            down(mPath +page);
                xrecycleview.postDelayed(new Runnable() {
                    @Override
                    public void run() {
           xrecycleview.loadMoreComplete();
                    }
                },3000);
            }
        });


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_zxcomm_b;
    }


    @Override
    public void hideProgress() {
if (xrecycleview!=null) {
    xrecycleview.refreshComplete();
}


    }

    @Override
    public void setApaptData(ZXZp zxZp) {
          page = zxZp.getPageNum();
        zxRecycleAdapter.setData(zxZp.getData(),getFActivity());
    }
//下载
    public  void  down(String url){
        String zxlz = url;
        zxRecycleBiz.loadJsonData(getFActivity(),zxlz);
    }

}
